import { count } from './mathjs.ts';
//计算产品包装
export const packageDescription = (row: any, number: number): any => {
  let text = '';
  let remainder = 0; //余箱数
  let cases = 0;
  let actualCartons = 0;
  const productStyle = row.productStyle; //产品类型
  const productUnit = row.productUnit || '枚'; //产品单位
  const pageEqualTags = Number(row.pageEqualTags) || 0; //每张枚数
  const packageEqualPages = Number(row.packageEqualPages) || 0; //每包张数【平张和折页有该属性】
  const eachBoxQuantity = Number(row.eachBoxQuantity) || 0; //每箱数量
  const eachBoxNum: number = count(`${pageEqualTags}*${[2, 4].includes(productStyle) ? 1 : packageEqualPages || 0}*${eachBoxQuantity}`) || 0; //每箱总数量

  if (number > 0) {
    cases = Math.floor(number / eachBoxNum); //整箱箱数
    actualCartons = Number(cases); //整箱箱数
    remainder = number % eachBoxNum; //余箱数
    if (cases > 0 && eachBoxNum > 0) {
      text += `(${CompletePackaging(productStyle, productUnit, pageEqualTags, packageEqualPages, eachBoxQuantity)})*${cases}箱`;
      if (remainder > 0) {
        text += `+`;
      }
    }
    if (remainder > 0) {
      actualCartons++;
      text += `(${IncompletePackaging(productStyle, productUnit, pageEqualTags, packageEqualPages, eachBoxQuantity, remainder)})*1箱`;
    }
  }
  return {
    text: text, //完整描述
    fullCartonCount: cases, //整箱箱数
    actualCartonCount: actualCartons, //实际箱数
    completePackaging: CompletePackaging(productStyle, productUnit, pageEqualTags, packageEqualPages, eachBoxQuantity), //整箱描述
    incompletePackaging: IncompletePackaging(productStyle, productUnit, pageEqualTags, packageEqualPages, eachBoxQuantity, remainder), //散箱描述
    bulkQuantity: remainder || null, //散箱数量
    fullCartonQuantity: eachBoxNum || null, //整箱数量
  };
  //完整箱数
  function CompletePackaging(productStyle: number, productUnit: string, pageEqualTags: number, packageEqualPages: number, eachBoxQuantity: number) {
    if ([2, 4].includes(productStyle)) {
      return `${pageEqualTags}${productUnit}/卷*${eachBoxQuantity}`;
    } else {
      return `${count(`${pageEqualTags}*${packageEqualPages || 1}`)}${productUnit}/包*${eachBoxQuantity}`;
    }
  }
  //不完整箱数
  function IncompletePackaging(productStyle: number, productUnit: string, pageEqualTags: number, packageEqualPages: number, eachBoxQuantity: number, remainderNum: number) {
    const packageEqualPagesNum = count(`${pageEqualTags}*${packageEqualPages || 1}`) || 1; //每包总数量
    const packageEqualPagesCases = Math.floor(remainderNum / packageEqualPagesNum); //整包包数
    const cases = remainderNum % packageEqualPagesNum; //整包
    const eachBoxQuantityPagesCases = Math.floor(cases / packageEqualPagesNum); //整包包数
    const cases1 = remainderNum % packageEqualPagesNum; //整包
    let text = '';
    switch (productStyle) {
      case 2: //卷式
      case 4: //手账
        if (packageEqualPagesCases > 0) {
          text += `${pageEqualTags}${productUnit}/卷*${packageEqualPagesCases}`;
          if (cases > 0) {
            text += `+`;
          }
        }
        if (cases > 0) {
          text += `${cases}${productUnit}/卷*1`;
        }
        break;
      case 1: //平张
      case 3: //折页
        //是否满足整包
        if (packageEqualPagesCases > 0) {
          text += `${packageEqualPagesNum}${productUnit}/包*${packageEqualPagesCases}`;
          if (cases1 > 0) {
            text += `+`;
          }
        }
        if (cases1 > 0 || eachBoxQuantityPagesCases > 0) {
          text += `${cases1}${productUnit}/包*1`;
        }
        break;
    }
    return text;
  }
};
